package com.ttl.bc.ap.purchaseorder.action;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;


import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderBean;
import com.ttl.bc.ap.purchaseorder.db.BCPurchaseOrderBO;
import com.ttl.bc.ap.purchaseorder.db.BCPurchaseOrderDAO;
import com.ttl.bc.ap.stkrequest.BCStkRequestBO;
import com.ttl.bc.ap.stkrequest.BCStkRequestForm;
import com.ttl.bc.report.purchasing.PurchasingReportBO;
import com.ttl.common.util.CommonUtils;
import com.ttl.common.util.Page;

public class BCPurchaseOrderAction extends DispatchAction {

	public ActionForward blank_po(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :blank_po");		
		String docNo 		 = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
		
		request.setAttribute("docNo", docNo);
		request.setAttribute("ttl_purchaseOrderDocNo", docNo.substring(6));
		return mapping.findForward("blank_po");
	}
	
	public ActionForward addpo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :add");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.addpo(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			
			return mapping.findForward("success_add_po");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_add_po");
		}
	}

	public ActionForward addpo3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :add");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.addpo3(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			
			return mapping.findForward("success_add_po");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_add_po");
		}
	}	
	
	public ActionForward updatepo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :update");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.updatepo(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			
			return mapping.findForward("success_update_po");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_update_po");
		}
	}	

	public ActionForward deletepo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :deletepo");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.deletepo(form, request, response)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			
			return mapping.findForward("success_delete_po");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete_po");
		}
	}	
	
	public ActionForward printPO(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :printPO");		
				
		return mapping.findForward("blank_printpo");
	}
	
	//ค้นหา PO=Purchase Order
	public ActionForward poFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :poFind ");
	
		DynaActionForm dynaForm = (DynaActionForm)form;
		@SuppressWarnings("unused")
		String docNo2 = ""+dynaForm.getString("docNo2");
		String itemFind = ""+dynaForm.getString("itemFind");
		
		if(itemFind!=null) {
			itemFind = itemFind.trim(); 
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");		//แปลงให้ค้นหาภาษาไทยได้
		}		
		request.setAttribute("itemFind", itemFind);		
		
		String hql = "FROM BCPurchaseOrderBean " +
				"WHERE (docNo like '%"+itemFind+"%' or ittl_purchaseCode like '%"+itemFind+"%' or tempMyDesc2 like '%"+itemFind+"%' ) " +
				"and ( docNo like 'PO1%' or docNo like 'PO2%') "+
				"and isCancel=0 "+
				"ORDER BY docNo";
//		new BCAPInvoiceBO().setPage(request, hql);
		System.out.println(hql);
		Page.setPage(request, hql);
		
		return mapping.findForward("success_poFind");
	}	
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :showFind");
		
		new BCPurchaseOrderBO().showFind(request);
				
		return mapping.findForward("success_showFind2");
	}
	
	public ActionForward displayPO(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use:BCPurchaseOrderAction #mode:displayPO");
			
/*		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		ArrayList results = (ArrayList)bo.displayPO(request);
		
		request.setAttribute("results_displayPO", results);	*/					
		request.setAttribute("docNoFind", request.getParameter("docNoFind"));
		
		new BCPurchaseOrderBO().displayPO2(request);
		
		return mapping.findForward("success_displayPO");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward findAll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use:BCPurchaseOrderAction #mode:findAll");
			
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
//		ArrayList resultsOLD2 = (ArrayList)bo.findAllOLD2(request);
		ArrayList results = (ArrayList)bo.findAll(request);
		
//		request.getSession().setAttribute("results", results);						
		request.setAttribute("results", results);
				
		return mapping.findForward("success_findAll");
	}	

	
	
	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
//		BCStkRequestBO bo = new BCStkRequestBO();
//		BCStkRequestForm reqForm2 = (BCStkRequestForm)bo.findForm(request);
		//ต้องให้ไปหาใน BCStkRequest,BCStkRequestSub เพราะจะต้อง update ittl_status ของ item นั้นๆด้วย
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findStkRequestForm(request);
		
//		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :cancel "+
									  "#docNo:"+reqForm.getDocNo());
		
		BCPurchaseOrderBO bo2 = new BCPurchaseOrderBO();
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo2.findForm(request);
		
		request.setAttribute("reqForm", reqForm);
//		request.setAttribute("inPage", inPage);
		request.setAttribute("poForm", poForm);
//		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("success_cancel");		
	}
	
	public ActionForward cancel3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {		
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" " +
				"#use :BCPurchaseOrderAction #mode :cancel3");
		
		BCPurchaseOrderBO po = new BCPurchaseOrderBO();
		BCPurchaseOrderBean results = po.findPO(form, request);
		
		request.setAttribute("po", results);		

		return mapping.findForward("success_cancel3");		
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

//		BCStkRequestBO bo = new BCStkRequestBO();
//		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findForm(request);
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findStkRequestForm(request);
		
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :edit "+
									  "#docNo:"+reqForm.getDocNo());
		
		BCPurchaseOrderBO bo2 = new BCPurchaseOrderBO();
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo2.findForm(request);
		
//		String poDocNo = (String)request.getParameter("poDocNo");
//		if(poDocNo !=null) {			
//			request.setAttribute("changePO", poDocNo);
//		}				
		request.setAttribute("reqForm", reqForm);		
		request.setAttribute("poForm", poForm);		
				
		return mapping.findForward("blank_edit");
	}
	
	
	public ActionForward edit3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {		
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" " +
				"#use :BCPurchaseOrderAction #mode :edit3");
		
		BCPurchaseOrderBO po = new BCPurchaseOrderBO();
		BCPurchaseOrderBean results = po.findPO(form, request);
		
		request.setAttribute("po", results);
//		request.setAttribute("countId", results2.size());		

		return mapping.findForward("success_edit3");		
	}	
	
	public ActionForward editPO(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

//		BCStkRequestBO bo = new BCStkRequestBO();
//		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findForm(request);
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo.findPurchaseOrderForm(request);
		
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :editPO "+
									  "#docNo:"+poForm.getPoDocNo());
		
//		BCPurchaseOrderBO bo2 = new BCPurchaseOrderBO();
//		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo2.findForm(request);
//		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo2.find2Form(request);
		
//		String poDocNo = (String)request.getParameter("poDocNo");
//		if(poDocNo !=null) {			
//			request.setAttribute("changePO", poDocNo);
//		}				
//		request.setAttribute("reqForm", reqForm);		
		request.setAttribute("poForm", poForm);		
				
		return mapping.findForward("blank_editPO");
	}		

	public ActionForward cancelPO(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {


		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)bo.findPurchaseOrderForm(request);
		
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :cancelPO "+
									  "#docNo:"+poForm.getPoDocNo());
		
		request.setAttribute("poForm", poForm);		
		request.setAttribute("isDyeChem", request.getParameter("isDyeChem"));		
		return mapping.findForward("blank_cancelPO");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		BCStkRequestBO bo = new BCStkRequestBO();
		BCStkRequestForm reqForm = (BCStkRequestForm)bo.findForm(request);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :create "+
									  "#docNo:"+reqForm.getDocNo());
		
		String purchaseOrderDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
		request.setAttribute("reqForm", reqForm);
		request.setAttribute("inPage", inPage);
		request.setAttribute("purchaseOrderDocNo", purchaseOrderDocNo);
		request.setAttribute("ttl_purchaseOrderDocNo", purchaseOrderDocNo.substring(6));
		request.getSession().setAttribute("userName", user);		
		
		return mapping.findForward("success_create");		
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward create3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {		
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :create3");

		String purchaseOrderDocNo = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
		request.setAttribute("docNo", purchaseOrderDocNo);		
		
		request.setAttribute("tempMyDesc2", purchaseOrderDocNo.substring(6, purchaseOrderDocNo.length()));
//		BCAPInvoiceBO bo = new BCAPInvoiceBO();		
//		ArrayList results = (ArrayList)bo.findPO(form, request);
		
		BCPurchaseOrderBO po = new BCPurchaseOrderBO();
		ArrayList results2 = (ArrayList)po.findPR(form, request);
		
		request.setAttribute("resultList_prpo", results2);
		request.setAttribute("countId", results2.size());		

		return mapping.findForward("success_create3");		
	}	
	
	public ActionForward blank(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String user 	 = (String)request.getSession().getAttribute("userName");				
		@SuppressWarnings("unused")
		String preDocNo  = (String)request.getSession().getAttribute("preDocNo");				
						
		String docNo     = CommonUtils.getMaxBCPurchaseOrderDocNo("PO1-");
		
		request.getSession().setAttribute("userName", user);
		request.getSession().setAttribute("docNo", docNo);		
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :blank");
		
		return mapping.findForward("blank");
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward showDataPO(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		BCPurchaseOrderForm poForm =(BCPurchaseOrderForm)form; 
		request.setAttribute("apCode", poForm.getApCode());
		
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :showDataPO");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();							
		ArrayList results = (ArrayList)bo.findOLD(form, request);
		BCPurchaseOrderFullForm fullForm = bo.setPOForm(results);
		
//		System.out.println("fullForm.getItemCode1 = "+fullForm.getItemCode1());
//		System.out.println("fullForm.getItemCode2 = "+fullForm.getItemCode2());
//		System.out.println("fullForm.getItemCode3 = "+fullForm.getItemCode3());
		
		request.setAttribute("fullForm", fullForm);		
		
		return mapping.findForward("success_showDataPO");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :add");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.add(form, request, response)==true) {
//			if(request.getParameter("changePO")==null) {				
				request.setAttribute("itemFind", request.getParameter("docNo"));
				return mapping.findForward("success_add");
//			}else {				
//				BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)form;				
//				request.setAttribute("docNoFind", poForm.getPoDocNo());
//				return mapping.findForward("success_changePO");
//			}
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_add");
		}
	}
	
	public ActionForward update3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :add");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.update3(form, request, response)==true) {
			request.setAttribute("message", "แก้ไขข้อมูลเรียบร้อย");
			
			return mapping.findForward("success_update_po3");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_update_po3");
		}
	}
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :add");
		
		System.out.println("poDocNo = "+request.getParameter("poDocNo"));
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.update(form, request, response)==true) {
//			System.out.println("\n\nchangePO="+request.getParameter("changePO"));
//			if(request.getParameter("changePO")==null ||
//					(String)request.getParameter("changePO")=="") {
//				
//				System.out.println("update in changePO==null");
				request.setAttribute("docNoFind", request.getParameter("docNo"));				
				return mapping.findForward("success_update");
//			}else {
//				
//				System.out.println("update in changePO!=null");
//				
//				BCPurchaseOrderForm poForm = (BCPurchaseOrderForm)form;				
//				request.setAttribute("docNoFind", poForm.getPoDocNo());
//				return mapping.findForward("success_changePO_update");				
//			}
			
			
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_update");
		}
	}	

	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :delete");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.delete(form, request, response)==true) {							
			return mapping.findForward("success_delete");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete");
		}
	}	
	
	public ActionForward delete3(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :delete");
		
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();						
		if (bo.delete3(form, request, response)==true) {
			request.setAttribute("message", "ยกเลิกข้อมูลเรียบร้อย");
			
			return mapping.findForward("success_delete_po3");
		}else {
			//ทำ map กับการ fail_add
			return mapping.findForward("fail_delete_po3");
		}
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward print(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String docNoParam = (String)request.getParameter("docNo");
//		String stkDocNoParam = (String)request.getParameter("stkDocNo");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :print ");		
		System.out.println("#docNo = "+docNoParam);
		
//start จะทดสอบส่งค่า parameter แบบ list 		
		String hql = "from BCPurchaseOrderBean where docNo = '"+docNoParam+"'";
		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
		ArrayList<BCPurchaseOrderBean> results = (ArrayList<BCPurchaseOrderBean>)bo.findPrint(hql);
		
		String nameOfReport="";

		for (BCPurchaseOrderBean p : results) {
			if(p.getBcPurchaseOrderSubs().size()==1) {
				nameOfReport = "/ireport/poForm_dyeChem3new.jasper";	
			}else {
				nameOfReport = "/ireport/poForm_dyeChem3.jasper";
			}
		}
//end จะทดสอบส่งค่า parameter แบบ list
		
		docNoParam = " po.docNo = '"+docNoParam+"'";
		System.out.println("docNoParam= "+docNoParam);
		
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
//		hashMap.put("stkDocNoParam", stkDocNoParam);
		
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = getServlet().getServletConfig()
//							.getServletContext().getResourceAsStream(nameOfReport);
//		
//		response.setContentType("application/pdf");		
//		//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
////		connection = DriverManager.getConnection("jdbc:odbc:ttl51","sa","admin@123");
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
//		System.out.println("connect jTDS ok");
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();

		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			new BCPurchaseOrderDAO().addPrintCount(docNoParam);
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
		
		
		return null;			
	}
	
	@SuppressWarnings("unchecked")
	public ActionForward printContinue(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		String docNoParam = (String)request.getParameter("docNo");
//		String stkDocNoParam = (String)request.getParameter("stkDocNo");
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.print(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCPurchaseOrderAction #mode :printContinue ");		
		
		
		DynaActionForm dynaForm = (DynaActionForm)form;
		String docNo1 = dynaForm.getString("docNo1");
		String docNo2 = dynaForm.getString("docNo2");
		
/*		ArrayList docNo1List = (ArrayList)new BCPurchaseOrderDAO().find("FROM BCPurchaseOrderQuery WHERE docNo = '"+docNo1+"'");
		int rowOrder1=0, rowOrder2=0;
		for (int i = 0; i < docNo1List.size(); i++) {
			BCPurchaseOrderQuery poQ = (BCPurchaseOrderQuery)docNo1List.get(i);
			rowOrder1 = poQ.getRowOrder().intValue();
		}
		ArrayList docNo2List = (ArrayList)new BCPurchaseOrderDAO().find("FROM BCPurchaseOrderQuery WHERE docNo = '"+docNo2+"'");
		for (int i = 0; i < docNo2List.size(); i++) {
			BCPurchaseOrderQuery poQ = (BCPurchaseOrderQuery)docNo2List.get(i);
			rowOrder2 = poQ.getRowOrder().intValue();			
		}		
		
		docNoParam = " po.rowOrder>= "+rowOrder1+" and po.rowOrder<="+rowOrder2;
		*/
		
		docNoParam = " po.docNo in (";
		
		
		
		String[] startDocNo = docNo1.split("-");
		String[] endDocNo = docNo2.split("-");
		String preDocNo = "";//startDocNo[0]+"-"+startDocNo[1]+"-"; 
			
		int countDocNo = 0;//Integer.valueOf(endDocNo[2])-Integer.valueOf(startDocNo[2])+1;
		NumberFormat formatter = new DecimalFormat("0000");
		int index1 = docNo1.indexOf("DC");
		
		if(index1!=-1) {// found "DC" in docNo1
			preDocNo = startDocNo[0]+"-"+startDocNo[1]+"-"+startDocNo[2]+"-";
			
			formatter = new DecimalFormat("000");
			countDocNo = Integer.valueOf(endDocNo[3])-Integer.valueOf(startDocNo[3])+1;
			System.out.println("countDocNo = "+countDocNo);
			for(int i=0; i<countDocNo; i++){
				
				int docs = (Integer.parseInt(startDocNo[3])+i);
				System.out.println("docs= "+docs);
				String docs2 = ""+formatter.format(docs);
				docNoParam+="'"+preDocNo+docs2+"',";
			}			
			
		}else {
			preDocNo = startDocNo[0]+"-"+startDocNo[1]+"-";
			
			formatter = new DecimalFormat("0000");
			countDocNo = Integer.valueOf(endDocNo[2])-Integer.valueOf(startDocNo[2])+1;
			System.out.println("countDocNo = "+countDocNo);
			for(int i=0; i<countDocNo; i++){	
				int docs = (Integer.parseInt(startDocNo[2])+i);
				System.out.println("docs= "+docs);
				String docs2 = ""+formatter.format(docs);
				docNoParam+="'"+preDocNo+docs2+"',";
			}
		}
		

		docNoParam = docNoParam.substring(0, docNoParam.length()-1  ) +")";
		
		System.out.println(docNoParam);
//start จะทดสอบส่งค่า parameter แบบ list 		
//		String hql = "from BCPurchaseOrderBean where docNo = '"+docNoParam+"'";
//		BCPurchaseOrderBO bo = new BCPurchaseOrderBO();
//		ArrayList results = (ArrayList)bo.findPrint(hql);
//end จะทดสอบส่งค่า parameter แบบ list
		String nameOfReport="";
//		if(!stkDocNoParam.equalsIgnoreCase("null")) {
//			nameOfReport = "/ireport/poForm.jasper";
//			System.out.println("stkDocNo !=null");
//		}else {
			nameOfReport = "/ireport/poForm_dyeChem3.jasper";
//			System.out.println("stkDocNo ==null");
//		}
							
		HashMap hashMap = new HashMap();
		hashMap.put("docNoParam", docNoParam);
		
//		Connection connection;
//		ServletOutputStream servletOutputStream = response.getOutputStream();
//		
//		InputStream reportStream = getServlet().getServletConfig()
//							.getServletContext().getResourceAsStream(nameOfReport);
//		
//		response.setContentType("application/pdf");		
//		//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//		Class.forName("net.sourceforge.jtds.jdbc.Driver");		
////		connection = DriverManager.getConnection("jdbc:odbc:ttl51","sa","admin@123");
//		connection = DriverManager.getConnection("jdbc:jtds:sqlserver://192.168.1.2:1433/ttl51;user=sa;password=admin@123");
////		System.out.println("connect jTDS ok");
//		JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, hashMap, connection);
//
//		connection.close();
//		servletOutputStream.flush();
//		servletOutputStream.close();
		
		if(CommonUtils.runReportToPdf(response, getServlet(), hashMap, nameOfReport)==true) {
			new BCPurchaseOrderDAO().addPrintCount(docNoParam);
			System.out.println("run report to pdf: complete");
		}else {
			System.out.println("run Report to pdf: can't run report");
		}
		
		
		return null;			
	}	
	
	public ActionForward printPO2(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String param = (String)request.getParameter("param");

		if(param==null) {
			return mapping.findForward("blank_printpo");
		}
		
		if(param.equalsIgnoreCase("poOfAp")) {
			new PurchasingReportBO().getPO(form, request, response, getServlet());
		} 		
						
		return null;					
	}	
}
